﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using Maticsoft.DBUtility;
using MySql.Data.MySqlClient;
namespace XinKuaiJi.DAL
{
    //t_user_info
    public partial class t_user_info
    {

        public bool Exists(long USER_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_user_info");
            strSql.Append(" where ");
            strSql.Append(" USER_ID = @USER_ID  ");
            MySqlParameter[] parameters = {
					new MySqlParameter("@USER_ID", MySqlDbType.Int64)
			};
            parameters[0].Value = USER_ID;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(XinKuaiJi.Model.t_user_info model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_user_info(");
            strSql.Append("USER_NAME,PASSWORD,EMAIL,CREATE_TIME,STATUS,ROLE_ID,CHANNEL_ID,UPDATE_TIME,DES1,DES2,DES3");
            strSql.Append(") values (");
            strSql.Append("@USER_NAME,@PASSWORD,@EMAIL,@CREATE_TIME,@STATUS,@ROLE_ID,@CHANNEL_ID,@UPDATE_TIME,@DES1,@DES2,@DES3");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            MySqlParameter[] parameters = {
			            new MySqlParameter("@USER_NAME", MySqlDbType.VarChar,30) ,            
                        new MySqlParameter("@PASSWORD", MySqlDbType.VarChar,45) ,            
                        new MySqlParameter("@EMAIL", MySqlDbType.VarChar,100) ,            
                        new MySqlParameter("@CREATE_TIME", MySqlDbType.DateTime) ,            
                        new MySqlParameter("@STATUS", MySqlDbType.VarChar,1) ,            
                        new MySqlParameter("@ROLE_ID", MySqlDbType.Int64,20) ,            
                        new MySqlParameter("@CHANNEL_ID", MySqlDbType.Int64,20) ,            
                        new MySqlParameter("@UPDATE_TIME", MySqlDbType.DateTime) ,            
                        new MySqlParameter("@DES1", MySqlDbType.VarChar,200) ,            
                        new MySqlParameter("@DES2", MySqlDbType.VarChar,200) ,            
                        new MySqlParameter("@DES3", MySqlDbType.VarChar,200)             
              
            };

            parameters[0].Value = model.USER_NAME;
            parameters[1].Value = model.PASSWORD;
            parameters[2].Value = model.EMAIL;
            parameters[3].Value = model.CREATE_TIME;
            parameters[4].Value = model.STATUS;
            parameters[5].Value = model.ROLE_ID;
            parameters[6].Value = model.CHANNEL_ID;
            parameters[7].Value = model.UPDATE_TIME;
            parameters[8].Value = model.DES1;
            parameters[9].Value = model.DES2;
            parameters[10].Value = model.DES3;

            object obj = DbHelperMySQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {

                return Convert.ToInt64(obj);

            }

        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(XinKuaiJi.Model.t_user_info model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_user_info set ");

            strSql.Append(" USER_NAME = @USER_NAME , ");
            strSql.Append(" PASSWORD = @PASSWORD , ");
            strSql.Append(" EMAIL = @EMAIL , ");
            strSql.Append(" CREATE_TIME = @CREATE_TIME , ");
            strSql.Append(" STATUS = @STATUS , ");
            strSql.Append(" ROLE_ID = @ROLE_ID , ");
            strSql.Append(" CHANNEL_ID = @CHANNEL_ID , ");
            strSql.Append(" UPDATE_TIME = @UPDATE_TIME , ");
            strSql.Append(" DES1 = @DES1 , ");
            strSql.Append(" DES2 = @DES2 , ");
            strSql.Append(" DES3 = @DES3  ");
            strSql.Append(" where USER_ID=@USER_ID ");

            MySqlParameter[] parameters = {
			            new MySqlParameter("@USER_ID", MySqlDbType.Int64,20) ,            
                        new MySqlParameter("@USER_NAME", MySqlDbType.VarChar,30) ,            
                        new MySqlParameter("@PASSWORD", MySqlDbType.VarChar,45) ,            
                        new MySqlParameter("@EMAIL", MySqlDbType.VarChar,100) ,            
                        new MySqlParameter("@CREATE_TIME", MySqlDbType.DateTime) ,            
                        new MySqlParameter("@STATUS", MySqlDbType.VarChar,1) ,            
                        new MySqlParameter("@ROLE_ID", MySqlDbType.Int64,20) ,            
                        new MySqlParameter("@CHANNEL_ID", MySqlDbType.Int64,20) ,            
                        new MySqlParameter("@UPDATE_TIME", MySqlDbType.DateTime) ,            
                        new MySqlParameter("@DES1", MySqlDbType.VarChar,200) ,            
                        new MySqlParameter("@DES2", MySqlDbType.VarChar,200) ,            
                        new MySqlParameter("@DES3", MySqlDbType.VarChar,200)             
              
            };

            parameters[0].Value = model.USER_ID;
            parameters[1].Value = model.USER_NAME;
            parameters[2].Value = model.PASSWORD;
            parameters[3].Value = model.EMAIL;
            parameters[4].Value = model.CREATE_TIME;
            parameters[5].Value = model.STATUS;
            parameters[6].Value = model.ROLE_ID;
            parameters[7].Value = model.CHANNEL_ID;
            parameters[8].Value = model.UPDATE_TIME;
            parameters[9].Value = model.DES1;
            parameters[10].Value = model.DES2;
            parameters[11].Value = model.DES3;
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long USER_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_user_info ");
            strSql.Append(" where USER_ID=@USER_ID");
            MySqlParameter[] parameters = {
					new MySqlParameter("@USER_ID", MySqlDbType.Int64)
			};
            parameters[0].Value = USER_ID;


            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string USER_IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_user_info ");
            strSql.Append(" where ID in (" + USER_IDlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public XinKuaiJi.Model.t_user_info GetModel(long USER_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select USER_ID, USER_NAME, PASSWORD, EMAIL, CREATE_TIME, STATUS, ROLE_ID, CHANNEL_ID, UPDATE_TIME, DES1, DES2, DES3  ");
            strSql.Append("  from t_user_info ");
            strSql.Append(" where USER_ID=@USER_ID");
            MySqlParameter[] parameters = {
					new MySqlParameter("@USER_ID", MySqlDbType.Int64)
			};
            parameters[0].Value = USER_ID;


            XinKuaiJi.Model.t_user_info model = new XinKuaiJi.Model.t_user_info();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["USER_ID"].ToString() != "")
                {
                    model.USER_ID = long.Parse(ds.Tables[0].Rows[0]["USER_ID"].ToString());
                }
                model.USER_NAME = ds.Tables[0].Rows[0]["USER_NAME"].ToString();
                model.PASSWORD = ds.Tables[0].Rows[0]["PASSWORD"].ToString();
                model.EMAIL = ds.Tables[0].Rows[0]["EMAIL"].ToString();
                if (ds.Tables[0].Rows[0]["CREATE_TIME"].ToString() != "")
                {
                    model.CREATE_TIME = DateTime.Parse(ds.Tables[0].Rows[0]["CREATE_TIME"].ToString());
                }
                model.STATUS = ds.Tables[0].Rows[0]["STATUS"].ToString();
                if (ds.Tables[0].Rows[0]["ROLE_ID"].ToString() != "")
                {
                    model.ROLE_ID = long.Parse(ds.Tables[0].Rows[0]["ROLE_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CHANNEL_ID"].ToString() != "")
                {
                    model.CHANNEL_ID = long.Parse(ds.Tables[0].Rows[0]["CHANNEL_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UPDATE_TIME"].ToString() != "")
                {
                    model.UPDATE_TIME = DateTime.Parse(ds.Tables[0].Rows[0]["UPDATE_TIME"].ToString());
                }
                model.DES1 = ds.Tables[0].Rows[0]["DES1"].ToString();
                model.DES2 = ds.Tables[0].Rows[0]["DES2"].ToString();
                model.DES3 = ds.Tables[0].Rows[0]["DES3"].ToString();

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM t_user_info ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM t_user_info ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperMySQL.Query(strSql.ToString());
        }


    }
}

